//
//  MKUITabBarItemIndicatorView.m
//  MKUI
//
//  Created by 苏尚进 on 2020/8/25.
//  Copyright © 2020 moon. All rights reserved.
//

#import "MKUITabBarItemIndicatorView.h"

@interface MKUITabBarItemIndicatorView ()
@property(nonatomic,strong) UIView *indicatorLine;
@end

@implementation MKUITabBarItemIndicatorView
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.indicatorLine = [[UIView alloc] init];
        self.indicatorColor = [UIColor systemBlueColor];
        self.indicatorHeight = 3;
        [self addSubview:self.indicatorLine];
        self.userInteractionEnabled = NO;
    }
    return self;
}
- (void)setIndicatorColor:(UIColor *)indicatorColor{
    self.indicatorLine.backgroundColor = indicatorColor;
}
- (UIColor *)indicatorColor{
    return self.indicatorLine.backgroundColor;
}
- (void)setIndicatorHeight:(CGFloat)indicatorHeight{
    _indicatorHeight = indicatorHeight;
    [self setNeedsLayout];
}
- (void)setContentInsets:(UIEdgeInsets)contentInsets{
    _contentInsets = contentInsets;
    [self setNeedsLayout];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    CGRect f = bounds;
    UIEdgeInsets insets = self.contentInsets;
    f.size.width = bounds.size.width-insets.left-insets.right;
    f.size.height = self.indicatorHeight;
    f.origin.x = insets.left;
    if(insets.top==0){
        f.origin.y = bounds.size.height-f.size.height-self.contentInsets.bottom;
    }else{
        f.origin.y = insets.top;
    }
    self.indicatorLine.frame = f;
}
@end

@implementation MKUITabBarItemViewContainerCollectionView(MKUITabBarItemViewContainerCollectionView)
- (MKUITabBarItemIndicatorView *)lineIndicatorView{
    if([self.itemIndicatorView isKindOfClass:[MKUITabBarItemIndicatorView class]]){
        return self.itemIndicatorView;
    }
    return nil;
}
@end
